package com.pandora.credit.mongo.dao.impl;

import com.pandora.credit.mongo.dao.MongoNewsDao;
import com.pandora.credit.mongo.entity.MongoNews;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author developer
 */
@Repository("mongoNewsDaoImpl")
public class MongoNewsDaoImpl implements MongoNewsDao {
    @Resource
    private MongoTemplate mongoTemplatePc;

    @Override
    public List<MongoNews> selectAllLimit(int length) {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC, "_id"));
        query.limit(length);
        List<MongoNews> li = mongoTemplatePc.find(query, MongoNews.class);
        return li;
    }

    @Override
    public List<MongoNews> selectByIdLimit(long id, int length) {
        Query query = new Query();
        Criteria criteria = Criteria.where("_id").is(id);
        query.addCriteria(criteria);
        query.with(new Sort(Sort.Direction.DESC, "_id"));
        query.limit(length);
        List<MongoNews> li = mongoTemplatePc.find(query, MongoNews.class);
        return li;
    }
}
